#!/usr/bin/env python
# yongming.li 

import os
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import string
from DnDListWidget import *


class SelectProcessForm(QDialog):

    def getallprocess(self):
        # adb shell ps
        self.processes=[]
        lines = os.popen('adb shell ps').readlines()
        for  line  in lines: 
            s=line.strip()
            item=string.split(s)
            if item[1]=='PID':
                continue
            size = len (item)
            self.processes.append(item[size-1])
        #print self.processes
    def ok(self):
        #QListWidgetItem-list QListWidget.items(self, QMimeData data)
        #items = self.dndIconListWidget.items(QMimeData())
        print '---------------------------------------------------'
        count = self.dndIconListWidget.count()
        for index in range(0,count):
            name = self.dndIconListWidget.item(index).text()
            print name
            if index==0:
                os.system('echo %s  > '%(name) + './out/monitorprocess.txt')
            else:
                os.system('echo %s  >> '%(name) + './out/monitorprocess.txt')
             

        
    def __init__(self, parent=None):
        super(SelectProcessForm, self).__init__(parent)

        dndListWidget = DnDListWidget()
        path = os.path.dirname(__file__)
        self.getallprocess()
        for index in range(0,len(self.processes)):
            item = QListWidgetItem(self.processes[index])
            item.setIcon(QIcon(os.path.join('', './images/process/%d.png'%(index%10))))
            dndListWidget.addItem(item)
 

        self.dndIconListWidget = DnDListWidget()
        self.dndIconListWidget.setMinimumSize(200,500)
        self.dndIconListWidget.setViewMode(QListWidget.IconMode)
        self.okbutton = QPushButton()
        self.okbutton.setText ('ok')
        self.connect(self.okbutton, SIGNAL('clicked()'), self.ok)
        layout = QGridLayout()
        layout.addWidget(dndListWidget, 0, 0)
        layout.addWidget(self.dndIconListWidget, 0, 1)
        layout.addWidget(self.okbutton, 1, 1)
        

        self.setLayout(layout)

        self.setWindowTitle("Custom Drag and Drop")

if __name__ == '__main__':
    app = QApplication(sys.argv)
    form = SelectProcessForm()
    form.show()
    app.exec_()

